rm(list=ls(all=T))
knitr::opts_chunk$set(comment = NA)
knitr::opts_knit$set(global.par = TRUE)
par(cex=0.7)
options(scipen=20, digits=5, width=80)
if(!require(dplyr)) install.packages("dplyr")
if(!require(tibble)) install.packages("tibble")
if(!require(ggplot2)) install.packages("ggplot2")
if(!require(GGally)) install.packages("GGally")
if(!require(plotly)) install.packages("plotly")
if(!require(ggpubr)) install.packages("ggpubr")
if(!require(pacman)) install.packages("pacman")Sys.setlocale(category="LC_ALL", locale="cht")
pacman::p_load(dplyr,tibble,GGally,plotly,ggpubr)
rm(list=ls(all=T))
load("D:/acer/Desktop/school/碩一/R class/L04/unit04/data/villages.rdata")
windowsFonts(Arial = windowsFont("Arial"))
windowsFonts(heiti = windowsFont("heiti"))
windowsFonts()
#install.packages("showtext")
library(showtext)
showtext_auto(enable=TRUE)
font_add(family = "heiti",regular = "msjh.ttc")
font.families()df_Town <- Town[c("COUNTY","TOWN","人均應稅所得")]
head(df_Town) COUNTY TOWN 人均應稅所得
宜蘭縣三星鄉 宜蘭縣 三星鄉 388.296
宜蘭縣大同鄉 宜蘭縣 大同鄉 898.713
宜蘭縣五結鄉 宜蘭縣 五結鄉 46.979
宜蘭縣冬山鄉 宜蘭縣 冬山鄉 132.619
宜蘭縣壯圍鄉 宜蘭縣 壯圍鄉 863.482
宜蘭縣宜蘭市 宜蘭縣 宜蘭市 232.244
nrow(df_Town)[1] 368
df_Town2 <- group_by(df_Town,COUNTY) %>%
summarise(人均應稅所得=sum(人均應稅所得))df_V <- V %>% filter(year == "106Y")
df_V1 <- df_V %>% select(COUNTY,男性人口數,女性人口數,`15歲以上碩士人口數`)
df_V2 <- df_V %>%
select(COUNTY,TOWN,男性人口數,女性人口數,`15歲以上碩士人口數`) %>%
mutate(性比例=男性人口數/女性人口數)#以各縣市做group by
df_V1 <- group_by(df_V1,COUNTY)%>%
summarise(性比例=(sum(男性人口數)/sum(女性人口數)),Master=sum(`15歲以上碩士人口數`))
head(df_V1) # A tibble: 6 x 3
COUNTY 性比例 Master
<chr> <dbl> <dbl>
1 宜蘭縣 1.02 18686
2 花蓮縣 1.03 12617
3 金門縣 1.00 8145
4 南投縣 1.05 18064
5 屏東縣 1.05 28089
6 苗栗縣 1.07 21758
nrow(df_V1)[1] 22
#合併兩張表
df <- merge(x = df_Town2, y = df_V1, by = "COUNTY")
#View(df)可以發現數值變數才能進行統計量的計算,人均應稅所得碩士人口數最大最小值差距很大,而平均值分別落在4401和58514。由性比例的平均值可以看出全台灣是男性人口數較多
summary(df) COUNTY 人均應稅所得 性比例 Master
Length:22 Min. : 786 Min. :0.915 Min. : 814
Class :character 1st Qu.: 3191 1st Qu.:0.984 1st Qu.: 15156
Mode :character Median : 4433 Median :1.028 Median : 22274
Mean : 4401 Mean :1.030 Mean : 58514
3rd Qu.: 5754 3rd Qu.:1.061 3rd Qu.: 86726
Max. :10007 Max. :1.329 Max. :259384
#tapply(df_V2$`人均應稅所得`, df_V2$`COUNTY`, summary)
tapply(df$`人均應稅所得`, df$`COUNTY`, median) %>% sort() 嘉義市 基隆市 新竹市 澎湖縣 金門縣 雲林縣 南投縣 嘉義縣
785.90 823.31 880.84 1591.72 2913.34 3185.19 3207.83 3277.92
臺東縣 桃園市 花蓮縣 連江縣 宜蘭縣 臺北市 彰化縣 屏東縣
3687.40 3844.02 4391.13 4474.31 4829.05 5127.65 5216.70 5726.76
臺中市 新北市 臺南市 高雄市 苗栗縣 新竹縣
5762.57 6088.65 6199.97 6947.50 7861.90 10007.12
tapply(df$`人均應稅所得`, df$`COUNTY`, mean)%>% sort() 嘉義市 基隆市 新竹市 澎湖縣 金門縣 雲林縣 南投縣 嘉義縣
785.90 823.31 880.84 1591.72 2913.34 3185.19 3207.83 3277.92
臺東縣 桃園市 花蓮縣 連江縣 宜蘭縣 臺北市 彰化縣 屏東縣
3687.40 3844.02 4391.13 4474.31 4829.05 5127.65 5216.70 5726.76
臺中市 新北市 臺南市 高雄市 苗栗縣 新竹縣
5762.57 6088.65 6199.97 6947.50 7861.90 10007.12
#tapply(df_V2$`性比例`, df_V2$`COUNTY`, summary)
tapply(df$`性比例`, df$`COUNTY`, median) %>% sort() 臺北市 嘉義市 新北市 臺中市 新竹市 高雄市 桃園市 臺南市 金門縣 基隆市
0.91478 0.94618 0.96071 0.97247 0.97550 0.98153 0.99201 0.99792 0.99983 1.00152
宜蘭縣 花蓮縣 彰化縣 新竹縣 屏東縣 南投縣 澎湖縣 苗栗縣 臺東縣 雲林縣
1.02369 1.03160 1.03949 1.04434 1.04694 1.05081 1.06392 1.06673 1.06797 1.07822
嘉義縣 連江縣
1.08391 1.32869
tapply(df$`性比例`, df$`COUNTY`, mean) %>% sort() 臺北市 嘉義市 新北市 臺中市 新竹市 高雄市 桃園市 臺南市 金門縣 基隆市
0.91478 0.94618 0.96071 0.97247 0.97550 0.98153 0.99201 0.99792 0.99983 1.00152
宜蘭縣 花蓮縣 彰化縣 新竹縣 屏東縣 南投縣 澎湖縣 苗栗縣 臺東縣 雲林縣
1.02369 1.03160 1.03949 1.04434 1.04694 1.05081 1.06392 1.06673 1.06797 1.07822
嘉義縣 連江縣
1.08391 1.32869
#tapply(df$`Master`, df$`COUNTY`, summary)
tapply(df$`Master`, df$`COUNTY`, median) %>% sort()連江縣 澎湖縣 臺東縣 金門縣 花蓮縣 嘉義縣 基隆市 嘉義市 南投縣 宜蘭縣 苗栗縣
814 4424 7044 8145 12617 15148 15182 15467 18064 18686 21758
雲林縣 屏東縣 新竹縣 新竹市 彰化縣 臺南市 桃園市 高雄市 臺中市 新北市 臺北市
22791 28089 37046 40448 48727 99393 109943 147299 148046 208796 259384
tapply(df$`Master`, df$`COUNTY`, mean) %>% sort()連江縣 澎湖縣 臺東縣 金門縣 花蓮縣 嘉義縣 基隆市 嘉義市 南投縣 宜蘭縣 苗栗縣
814 4424 7044 8145 12617 15148 15182 15467 18064 18686 21758
雲林縣 屏東縣 新竹縣 新竹市 彰化縣 臺南市 桃園市 高雄市 臺中市 新北市 臺北市
22791 28089 37046 40448 48727 99393 109943 147299 148046 208796 259384
從下面圖表可以發現到人均應稅所得多聚集在同一處,主要在3000-5500那段,不過有10000-12000的特殊極端值
x = df$人均應稅所得
par(mfrow=c(3,1),cex=0.8)
boxplot(x,horizontal=T) # 盒狀圖
hist(x) # 直方圖
plot(density(x)) # 密度函數
rug(x,col='blue') # 刻度線
從下面圖表可以發現到性別多聚集在同一處,主要在0.9-1.1那段(不過有一個離群直在1.3那段)
x = df$性比例
par(mfrow=c(3,1),cex=0.8)
boxplot(x,horizontal=T) # 盒狀圖
hist(x) # 直方圖
plot(density(x)) # 密度函數
rug(x,col='blue') # 刻度線
從下面圖表可以發現到性別多聚集在同一處,主要在0-50000那段(有一些離群值在10000-15000跟200000-300000)
x = df$Master
par(mfrow=c(3,1),cex=0.8)
boxplot(x,horizontal=T) # 盒狀圖
hist(x) # 直方圖
plot(density(x)) # 密度函數
rug(x,col='blue') # 刻度線
在這張圖中可以發現人均應稅所得新竹縣、苗栗縣、高雄市、台南市較高,而基隆市、新竹市、嘉義市、澎湖縣等較低。 在這張圖中可以發現碩士人口新北市、高雄市、桃園市、台北市、台中市、台南市較高,其餘則偏低。 在這張圖中可以發現在性別比的部分,連江縣重男輕女較為明顯,而台北市較少有偏見
g <- ggplot(df, aes(x =COUNTY))
p <- ggplot(df_V2)
ggarrange(
g + geom_col(aes(y=`人均應稅所得`,fill=COUNTY))+
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5,family="heiti")),
g + geom_point(aes(y=`性比例`,color=COUNTY))+
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5,family="heiti"))+
geom_hline(aes(yintercept=1,color="red")),
g + geom_col(aes(y=Master,fill=COUNTY))+
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5,family="heiti")),
common.legend=T)
看到盒狀圖的部分,以新北市為例,資料很分散,導致有許多離群值。 代表新北市有一半以上鄉鎮女性比例較高,但在其他鄉鎮中多數的鄉鎮男性比例特別高,因而導致有許多離群值產生
ggarrange(
g + geom_col(aes(y=`人均應稅所得`,fill=COUNTY))+
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5,family="heiti")),
p + geom_boxplot(aes(x=COUNTY,y=`性比例`,color=COUNTY))+
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5,family="heiti"))+
geom_hline(aes(yintercept=1,color="red")),
g + geom_col(aes(y=Master,fill=COUNTY))+
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5,family="heiti")),
common.legend=T)
性別比跟人均應稅所得呈現正相關,相關係數0.14362,相關性非常低,呈現弱相關,但單純從數字上來看一個城市男性越多,人均應稅所得越多
x = log10(df$`人均應稅所得`)
y = df$`性比例`
cor(x, y)[1] 0.14362
從第一張圖可以發現,新竹縣的人均應稅所得最多,嘉義的性別比最大,男性嚴重多於女性,台北的性別比最小,女性多於男性
g1<- ggplot(df,aes(x,y,color=COUNTY)) + geom_point() +
labs(x = '人均應稅所得',y = '性比例')+
geom_smooth(se=F,color='blue') +
geom_smooth(se=F,color='red',method='lm')
ggplotly(g1)15歲以上碩士人口跟人均應稅所呈現正相關,相關係數0.35369,中等相關,代表碩士人數越多確實有可能會提高人均收入
x = log10(df$`人均應稅所得`)
y = log10(df$`Master`)
cor(x, y)[1] 0.33276
從第二章圖可以發現,台北市碩士人口數最多,新竹縣收入最多。 碩士人數越多,縣市的人均應稅所得越高
g2 <- ggplot(df,aes(x,y,color=COUNTY)) + geom_point() +
labs(x = '人均應稅所得',y = 'Master')+
geom_smooth(se=F,color='blue') +
geom_smooth(se=F,color='red',method='lm')
ggplotly(g2)性別比跟15歲以上碩士人口呈現負相關,相關係數-0.76842,強烈相關,代表在台灣女生擁有碩士學位比例,相對於男性,非常少
x = log10(df$`Master`)
y = df$`性比例`
cor(x, y)[1] -0.76842
第三張圖可以發現,台北市碩士人口最多,擁有最小性別比,嘉義縣的性別比最高,碩士人口介於縣市平均
g3 <- ggplot(df,aes(x,y,color=COUNTY)) + geom_point() +
labs(x = 'Master',y = '性比例')+
geom_smooth(se=F,color='blue') +
geom_smooth(se=F,color='red',method='lm')
ggplotly(g3)從碩士人口數和人均應稅所得的關係圖發現,碩士人數越多人均應稅所得越高,再從性別比和碩士人口的關係圖中可以得知,目前男性擁有碩士學歷的人數比女性多,因此我們建議各縣市可多鼓勵女性,就讀碩士學歷,進而提高個人平均所得